<%@page import="weaver.general.Util"%>
<%@ page import="weaver.conn.RecordSet" %>
<%@ page language="java" contentType="text/html; charset=UTF-8" %>
<%@ include file="/systeminfo/init_wev8.jsp" %>
<jsp:useBean id="rs" class="weaver.conn.RecordSet" scope="page" />
<%
	int requestid = Util.getIntValue(request.getParameter("requestid"),0);//请求id
	int workflowid = Util.getIntValue(request.getParameter("workflowid"));//流程id
	int formid = Util.getIntValue(request.getParameter("formid"));//表单id
	int isbill = Util.getIntValue(request.getParameter("isbill"));//表单类型，1单据，0表单
	int nodeid = Util.getIntValue(request.getParameter("nodeid"));//流程的节点id
	int scoreid = Util.getIntValue(request.getParameter("scoreid"));//scoreid
	int type = Util.getIntValue(request.getParameter("type"));//0：查看；1：创建；2：编辑；3：监控
	int billid = Util.getIntValue(request.getParameter("billid"),0);//数据id
%>

<script type="text/javascript">



// =============


	jQuery(document).ready(function() {
		//企划管理编号
		var qhglbh = "#field9524";

		//绑定部门企画书管理编号改变事件
		jQuery(qhglbh).bindPropertyChange(function() {
			//删除所有明细节点
			deleteAllMx(0);
			//deleteAllMx(1);
			//deleteAllMx(2);
			//deleteAllMx(3);



			//初始化明细表id生成节点数值
			$G("indexnum0").value = 0;
			$G("indexnum1").value = 0;
			$G("indexnum2").value = 0;
			$G("indexnum3").value = 0;
			//企画管理编号
			var glbh = jQuery(qhglbh).val();
			console.log("企划管理编号变更：" + jQuery(qhglbh).val());
			//点击首先清空页面数据然后再插入,这里代码最后再写
			findAllMx(glbh);
		});


		/**
			* Ajax请求jsp获取数据
			*/
		function findAllMx(glbh) {
			jQuery.ajax({
				url: "/interface/sanden/workflow_updateDeptQH.jsp",
				dataType: "json", //传递的数据类型
				type: "post", //传递方式
				async: false,
				data: {
					"glbh": glbh
				},
				success: function(json) {
					//添加对应明细表 json[4][0]--明细表个数map
					var mxNum1 = json[4][0].num1;
					var mxNum2 = json[4][0].num2;
					var mxNum3 = json[4][0].num3;
					var mxNum4 = json[4][0].num4;
					//循环添加对应明细表
					addRows(json[4][0]);
					//添加明细表数据到明细表中
					addData(json);
				}
			});

		}


		//添加对应明细表
		function addRows(obj) {
			for (var i = 0; i < parseInt(obj.num1); i++) {
				addRow0(0)
			}
			for (var i = 0; i < parseInt(obj.num2); i++) {
				addRow1(1)
			}
			for (var i = 0; i < parseInt(obj.num3); i++) {
				addRow2(2)
			}
			for (var i = 0; i < parseInt(obj.num4); i++) {
				addRow3(3)
			}
		}

		//在对应明细表中添加对应数据
		function addData(obj) {
			//事前协议事项记录开始id
			var sqxyrq = "#field5999_"; //事前协议事项记录日期id
			var sqxysy = "#field6000_"; //事前协议事项记录事由id
			for (var i = 0; i < obj[0].length; i++) {
				//获取日期事由
				var rq = obj[0][i].rq;
				var sy = obj[0][i].sy;
				//页面赋值
				jQuery(sqxyrq + i).val(rq);
				jQuery(sqxyrq + i + "span").val(rq);
				jQuery(sqxysy + i).val(sy);

			}
			//重点项目说明开始id field6001_0
			var zdxmqhmc = "#field6001_"; //重点项目说明企划名称id
			var zdxmglqh = "#field6002_"; //重点项目管理企划
			var zdxmxzz = "#field6003_"; //重点项目现在值
			var zdxmmbz = "#field6004_" //重点项目目标值
			var zdxmdcqx = "#field6005_"; //重点项目达成年限
			for (var i = 0; i < obj[1].length; i++) {
				//获取 重点企画名称 	管理企画 	现在值 	目标值 	达成期限
				var zdxmmc = obj[1][i].zdxmmc;
				var glxm = obj[1][i].glxm;
				var xzz = obj[1][i].xzz;
				var mbz = obj[1][i].mbz;
				var dcqx98 = obj[1][i].dcqx98;
				//页面赋值
				jQuery(zdxmqhmc + i).val(zdxmmc);
				jQuery(zdxmglqh + i).val(glxm);
				jQuery(zdxmxzz + i).val(xzz);
				jQuery(zdxmmbz + i).val(mbz);
				jQuery(zdxmdcqx + i).val(dcqx98);

			}
			//费用说明开始id field6006_0
			var fysmfymc = "#field6006_"; //费用说明费用名称
			var fysmje = "#field6007_"; //费用说明金额
			var fysmbz = "#field6008_"; //费用说明备注
			for (var i = 0; i < obj[2].length; i++) {
				//获取 费用名称 	金额 	备注(需要补充或特别说明事项)
				var fymc = obj[2][i].fymc;
				var jee = obj[2][i].jee;
				var bz = obj[2][i].bz;
				//页面赋值
				jQuery(fysmfymc + i).val(fymc);
				jQuery(fysmje + i).val(jee);
				jQuery(fysmbz + i).val(bz);

			}
			//投资说明开始id field6009_0
			var tzsmtzfy = "#field6009_"; //投资说明投资费用
			var tzsmbz = "#field6010_"; //投资说明备注
			for (var i = 0; i < obj[3].length; i++) {
				//获取 投资费用 	备注(需要补充或特别说明事项)
				var tzmc99 = obj[3][i].tzmc99;
				var tzfy99 = obj[3][i].tzfy99;
				var bz = obj[3][i].bz;
				//页面赋值
				jQuery(tzsmtzfy + i).val(tzfy99);
				jQuery(fysmbz + i).val(bz);

			}
		}

		// 删除明细表方法
		//这里是可以优化的但是....因为已经可以运行了，就没有优化
		function deleteAllMx(id){
			jQuery("input[name='check_node_0']").each(function(){
					//选中选择框
					$(this).attr("checked","true");
					//执行删除方法
				});
				//调用删除节点代码
				delMx(0);
			jQuery("input[name='check_node_1']").each(function(){
					//选中选择框
					$(this).attr("checked","true");
					//执行删除方法
				});
				//调用删除节点代码
				delMx(1);
			jQuery("input[name='check_node_2']").each(function(){
					//选中选择框
					$(this).attr("checked","true");
					//执行删除方法
				});
				//调用删除节点代码
				delMx(2);
			jQuery("input[name='check_node_3']").each(function(){
					//选中选择框
					$(this).attr("checked","true");
					//执行删除方法
				});
				//调用删除节点代码
				delMx(3);
			}

		//删除明细重写方法
		function delMx(groupid){
		    var flag = false;
			var ids = document.getElementsByName("check_node_"+groupid);
			for(i=0; i<ids.length; i++) {
				if(ids[i].checked==true) {
					flag = true;
					break;
				}
			}
			var oTable=$G('oTable' + groupid);
			var len = document.forms[0].elements.length;
			var curindex=parseInt($G("nodesnum"+groupid).value);
			var i=0;
		var thead = jQuery('#oTable'+groupid).find('tr.exceldetailtitle').size();
		if(thead==null||thead==undefined||thead==0) thead=1;
			var rowsum1 = thead-1;
			var objname = "check_node_"+groupid;
			for(i=len-1; i >= 0;i--) {
				if (document.forms[0].elements[i].name==objname){
					rowsum1 += 1;
				}
			}
			for(i=len-1; i>=0; i--) {
				if(document.forms[0].elements[i].name==objname){
					if(document.forms[0].elements[i].checked==true){
						var nodecheckObj;
							var delid;
						try{
							if(jQuery(oTable.rows[rowsum1].cells[0]).find("[name='"+objname+"']").length>0){	
								delid = jQuery(oTable.rows[rowsum1].cells[0]).find("[name='"+objname+"']").eq(0).val(); 
							}
						}catch(e){}
						//记录被删除的旧记录 id串
						if(jQuery(oTable.rows[rowsum1].cells[0]).children().length>0 && jQuery(jQuery(oTable.rows[rowsum1].cells[0]).children()[0]).children().length>1){
							if($G("deldtlid"+groupid).value!=''){
								//老明细
								$G("deldtlid"+groupid).value+=","+jQuery(oTable.rows[rowsum1].cells[0].children[0]).children()[1].value;
							}else{
								//新明细
								$G("deldtlid"+groupid).value=jQuery(oTable.rows[rowsum1].cells[0]).children().eq(0).children()[1].value;
							}
						}
						//从提交序号串中删除被删除的行
						var submitdtlidArray=$G("submitdtlid"+groupid).value.split(',');
						$G("submitdtlid"+groupid).value="";
						var k;
						for(k=0; k<submitdtlidArray.length; k++){
							if(submitdtlidArray[k]!=delid){
								if($G("submitdtlid"+groupid).value==''){
									$G("submitdtlid"+groupid).value = submitdtlidArray[k];
								}else{
									$G("submitdtlid"+groupid).value += ","+submitdtlidArray[k];
								}
							}
						}
						oTable.deleteRow(rowsum1);
						curindex--;
					}
					rowsum1--;
				}
			}
			$G("nodesnum"+groupid).value=curindex;
				calSum(groupid);
		}

});


</script>